package com.onpositive.wikistorage;

import java.util.ArrayList;
import java.util.Collection;

import storage.v1.LowLevelStorage;

import com.onpositive.knowledge.model.IThing;
import com.onpositive.mediawiki.propertyparser.ParsedDocument;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.units.ParsedFileList;

public class PictureExtractor {
	
	public static PictureExtractor getInstance(){
		
		if( instance == null )
			instance = new PictureExtractor() ;
		
		return instance ;
	}
	
	private static PictureExtractor instance ;
	
	
	public void extractPictures( LowLevelStorage storage )
	{
		
		String workingPath = storage.getRootPath() ;
		
		IRealm<IThing> realm = storage.getRealm(null, null, null) ;		
		IProperty prop = storage.getProperty( ParsedDocument.PICTURE_LIST_PROPERTY_NAME ) ;
		
		for( Object obj : realm )
		{
			if( !(obj instanceof IThing) )
				continue ;
			
			String documentName = ((IThing) obj).getId() ;
			
			Object _values = prop.getValue(obj) ;
			if( !(_values instanceof Collection<?>) )
				continue ;
			
			Collection<?> values = (Collection<?>) _values ;
			
			for( Object val : values )
			{
				if( !(val instanceof ParsedFileList ) )
					continue ;
				
				@SuppressWarnings("unchecked")
				ArrayList<String> picList = (ArrayList<String>) ((ParsedFileList) val).value() ;
				PictureManager.getInstance(workingPath).grabPicturesFromNet(picList, documentName)	;			
			}
			
		}
		
	}

}
